<?
    class db
    {
        public $conn=NULL;
        public $result=NULL;
        public $host="localhost";
        public $user="root";
        public $password="";
        public $database="thinguyet";
        private $author="K H A";
        protected $version="1.1";
        function __construct()
        {
            $this->conn=mysql_connect($this->host,$this->user,$this->password);
            mysql_select_db($this->database,$this->conn);
            mysql_query("set names 'utf8'");
        }
        
    	protected function show_field_db($table) {
    		$query = " SHOW FIELDS FROM ".$table;
			$sql = mysql_query($query) or die(mysql_error());
            $array = array();
			while ($row = mysql_fetch_array($sql)) {
				$array[] = $row;
			}
			return $array;
        }
   		protected function get_data_db($query) {
   		 	$sql = mysql_query($query) or die(mysql_error());
            return mysql_fetch_array($sql);
        }
        protected function get_list_data_db($query) {
        	$sql = mysql_query($query) or die(mysql_error());
            $array = array();
			while ($row = mysql_fetch_array($sql)) {
				$array[] = $row;
			}
			return $array;
        }
	    function get_max_thutu_db($table) {
			$query = "SELECT thutu FROM ".$table." ORDER BY thutu DESC limit 0,1";
			$sql = mysql_query($query) or die(mysql_error());
			$details = mysql_fetch_array($sql);
			return $details['thutu'];
		}
	    protected function get_by_thutu_db($table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by thutu ASC ".$dk2;
                        //echo $query;
                        if ($id==-1)
				return $this->get_list_data_db($query);
			else 
				return $this->get_data_db($query);
		}
		protected function get_by_ngay_db($table,$id=-1,$anhien=-1) {
			settype($id, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) order by ngay DESC";
			if ($id==-1) 
				return $this->get_list_data_db($query);
			else 
				return $this->get_data_db($query);
		}
    	function get_by_parent_db($table,$table_parent,$field,$idparent) {
			$query = "SELECT ".$table.".* FROM ".$table.
			",".$table_parent." WHERE ".$table.".".$field."=".$table_parent.".id 
			and ".$table.".".$field."=".$idparent." order by thutu ASC";
			return $this->get_list_data_db($query);
		}
    	protected function get_max_count_db($table,$anhien=-1) {
			$query = "SELECT * FROM ".$table." WHERE 1 and (anhien='$anhien' or '$anhien'=-1) order by thutu ASC";
            return count($this->get_list_data_db($query));
		}
    	protected function get_by_thutu_limit_db($table,$offset,$records_per_page,$anhien=-1) {
			settype($offset, "int");
			settype($records_per_page, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE 1 and (anhien='$anhien' or '$anhien'=-1) order by thutu ASC LIMIT ".$offset.",".$records_per_page;
			return $this->get_list_data_db($query);
		}
    	protected function get_by_ngay_limit_db($table,$offset,$records_per_page,$anhien=-1) {
			settype($offset, "int");
			settype($records_per_page, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE 1 and (anhien='$anhien' or '$anhien'=-1) order by ngay DESC LIMIT ".$offset.",".$records_per_page;
			return $this->get_list_data_db($query);
		}
    	function get_short_page($table,$page,$page_count) {
    		settype($page, "int");
			settype($page_count, "int");
			if ($page_count>1) {
				if ($page-1 > 0)
					$prev = '<< <a href="?p='.$table.'_quanly&page='.($page-1).'">Prev</a>';
				else
					$prev = '<< Prev';
					
				if($page < $page_count)
					$next = '<a href="?p='.$table.'_quanly&page='.($page+1).'">Next</a> >>';
				else
					$next = 'Next >>';
					
				$page_select = '<select id="change_page_select" onchange="root_page(this.value)">';
				for($i=0;$i<$page_count ; $i++){
					if($page==$i+1)
						$page_select = $page_select.'<option value="?p='.$table.'_quanly&page='.($i+1).'" selected>'.($i+1).'</option>';
					else
						$page_select = $page_select.'<option value="?p='.$table.'_quanly&page='.($i+1).'">'.($i+1).'</option>';
				}
				$page_select = $page_select.'</select>';
					
				echo $prev.' | Page '.$page.'/'.$page_count.' | '.$next.'&nbsp;&nbsp;'.$page_select;
			}
		}
		
	    protected function update_thutu_db($table, $id, $thutu) {
			$query = "UPDATE ".$table." SET thutu='$thutu' WHERE id='$id'";
			mysql_query($query) or die(mysql_error());
		}
		
		protected function update_all_thutu_db($table, $thutu_cu, $thutu_moi) {
			$query = "UPDATE ".$table." SET thutu='$thutu_moi' WHERE thutu='$thutu_cu'";
			mysql_query($query) or die(mysql_error());
		}
		
	    function move_record_db($table, $id, $loai_move, $anhien=-1) {
			$details = $this->get_by_thutu_db($table,$id,$anhien);
			if (($loai_move=="up" || $loai_move=="down") && $details!="") {
				if ($loai_move=="up") 
					$thutu = $details['thutu']-1;
				if ($loai_move=="down") 
					$thutu = $details['thutu']+1;
				$this->update_all_thutu_db($table, $thutu, $details['thutu']);
				$this->update_thutu_db($table, $id, $thutu);
			}
		}
		
		function check_isset($check_isset) {
			foreach ($check_isset as $fieldname) { 
				if(!isset($_POST[$fieldname])) { 
					return false;
				} 
			} 
			return true;
		}
		
		function check_empty($check_empty) {
			foreach ($check_empty as $fieldname) { 
				if(empty ($_POST[$fieldname])) { 
					return false;
				} 
			}
			return true; 
		}
		
    	function update_field_db($table,$id,$field,$value,$anhien=-1) {
	   	 	$details = $this->get_by_thutu_db($table,$id,$anhien);
			if ($details!="") {
				$query = "UPDATE ".$table." SET ".$field."='$value' WHERE id='$id'";
				mysql_query($query) or die(mysql_error());
			}
        }
        
    	protected function change_anhien_db($table,$id) {
	   	 	$details = $this->get_by_thutu_db($table,$id,-1);
			if ($details!="") {
				if ($details['anhien']==0)
					$this->update_field_db($table,$id,"anhien",1);
				elseif ($details['anhien']==1)
					$this->update_field_db($table,$id,"anhien",0);
			}
        }
    	protected function change_anhien_list_db($table,$listid) {
        	$idlist = explode(",",$listid); 
			for ($i=0 ; $i<count($idlist) ; $i++){
				$id=$idlist[$i]; 
				$this->change_anhien_db($table,$id);
			}	
        }
		
   	 	function delete_data_db($table,$id) {
	   	 	$details = $this->get_by_thutu_db($table,$id,-1);
			if ($details!="") {
				$query = "DELETE FROM ".$table." WHERE id='$id'";
				mysql_query($query) or die(mysql_error());
			}
        }
    	function delete_list_data_db($table,$listid) {
        	$idlist = explode(",",$listid); 
			for ($i=0 ; $i<count($idlist) ; $i++){
				$id=$idlist[$i]; 
				$this->delete_data_db($table,$id);
			}	
        }
        
	    function check_valid_email($email) {
			$pattern = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
			if(preg_match($pattern, $email)==0) 
				return false;
			else 
				return true;
		}
           function pagesList($totalRows , $pageNum=1, $pageSize = 5, $offset = 5){
            $baseURL = $_SERVER['PHP_SELF'];
            parse_str($_SERVER['QUERY_STRING'],$arr);
            unset($arr['pageNum']);
            foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
            $baseURL .="?".$str;

            if ($totalRows<=0) return "";
            $totalPages = ceil($totalRows/$pageSize);
            if ($totalPages<=1) return "";

            $prevLink="";$nextLink="";

            if ($pageNum > 1) {
			$prevPage = $pageNum - 1;
			$prevLink="<span class='showpage'><a href='$baseURL&pageNum=$prevPage'>Trang trước</a></span>";
            }
            if ($pageNum < $totalPages) {
			$nextPage = $pageNum + 1;
			$nextLink = "<span class='showpage'><a href='$baseURL&pageNum=$nextPage'>Trang kế</a></span>";
            }

            $from = $pageNum - $offset;
            $to = $pageNum + $offset;
            if ($from <=0) { $from = 1;   $to = $offset*2; }
            if ($to > $totalPages) { $to = $totalPages; }
            $links = "";
            for($j = $from; $j <= $to; $j++) {
                    if ($j==$pageNum) $links= $links . "<span class='showpagePoint'>$j</span>";
                    else $links= $links . "<span class='showpageNum'><a href = '$baseURL&pageNum=$j'>$j</a></span>";
            } //for
            return $prevLink.$links.$nextLink;
            } // function pagesList
    }
?>